package jp.syuriken.snsw.terenaibot.format.function;

import jp.syuriken.snsw.terenaibot.format.CompileArg;
import jp.syuriken.snsw.terenaibot.format.FormatSyntaxException;
import jp.syuriken.snsw.terenaibot.format.PrecompiledSyntaxGroup;

/**
 * TODO snsoftware
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $ */
public class IntRemainFunction extends AbstractFormatFunction {
	
	@Override
	public String getReturnValue(PrecompiledSyntaxGroup[] args, CompileArg compileArg) throws FormatSyntaxException {
		checkArgLength(args, compileArg, "%", 2);
		int a;
		try {
			a = Integer.parseInt(args[0].format(compileArg));
		} catch (NumberFormatException e) {
			throw new FormatSyntaxException(String.format("Function<%%>: arg%d is not integer value: %s (format:%s)",
					0, args[0].format(compileArg), args[0].toString()));
		}
		int b;
		try {
			b = Integer.parseInt(args[1].format(compileArg));
		} catch (NumberFormatException e) {
			throw new FormatSyntaxException(String.format("Function<%%>: arg%d is not integer value: %s (format:%s)",
					1, args[1].format(compileArg), args[1].toString()));
		}
		if (a < 0) {
			throw new FormatSyntaxException(String.format(
					"Function<%%>: arg0 must be not negative number: %d (format:%s)", a, args[1].toString()));
		}
		if (b <= 0) {
			throw new FormatSyntaxException(String.format("Function<%%>: arg1 must be positive number: %d (format:%s)",
					b, args[1].toString()));
		}
		return String.valueOf(a % b);
	}
}
